﻿using System;
using System.Text;
using System.Text.RegularExpressions;

namespace UFrame
{
    public class StringTool
    {
        public static bool IsBoolean(string str)
        {
            return str.Equals("true", StringComparison.OrdinalIgnoreCase) ||
                   str.Equals("false", StringComparison.OrdinalIgnoreCase);
        }

        /// <summary>
        /// 计算汉字和英文总共多少个
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int CheckChatWordLength(string s)
        {
            //Debug.Log(s);
            if (string.IsNullOrEmpty(s))
            {
                return 0;
            }

            int len = 0;
            string regexNumber = "[\u4e00-\u9fa5]+"; //汉字正则表达式
            MatchCollection m = Regex.Matches(s, regexNumber);
            string newTemp = string.Empty;
            foreach (var item in m)
            {
                newTemp += item.ToString();
            }

            byte[] sAry = Encoding.Unicode.GetBytes(newTemp);
            len += sAry.Length;

            newTemp = string.Empty;
            regexNumber = "[^\u4e00-\u9fa5]+";
            m = Regex.Matches(s, regexNumber);
            foreach (var item in m)
            {
                newTemp += item.ToString();
            }

            sAry = Encoding.ASCII.GetBytes(newTemp);
            len += sAry.Length;
            return len;
        }


        /// <summary>
        /// 数字转中文
        /// </summary>
        /// <param name="number">eg: 22</param>
        /// <returns></returns>
        public static string NumberToChinese(int number)
        {
            string res = string.Empty;
            string str = number.ToString();
            string schar = str.Substring(0, 1);
            switch (schar)
            {
                case "1":
                    res = "一";
                    break;
                case "2":
                    res = "二";
                    break;
                case "3":
                    res = "三";
                    break;
                case "4":
                    res = "四";
                    break;
                case "5":
                    res = "五";
                    break;
                case "6":
                    res = "六";
                    break;
                case "7":
                    res = "七";
                    break;
                case "8":
                    res = "八";
                    break;
                case "9":
                    res = "九";
                    break;
                default:
                    res = "零";
                    break;
            }

            if (str.Length > 1)
            {
                switch (str.Length)
                {
                    case 2:
                    case 6:
                        res += "十";
                        break;
                    case 3:
                    case 7:
                        res += "百";
                        break;
                    case 4:
                        res += "千";
                        break;
                    case 5:
                        res += "万";
                        break;
                    default:
                        res += "";
                        break;
                }

                res += NumberToChinese(int.Parse(str.Substring(1, str.Length - 1)));
            }

            return res;
        }

        public static string FormatCopper(string num)
        {
            return FormatCopper(Convert.ToInt32(num));
        }

        public static string FormatCopper(double num)
        {
            double ratio = 10000;
            if (num >= ratio)
            {
                int w = (int) (num / ratio);
                return w + "万";
            }

            return num.ToString();
        }


        /// <summary>
        /// 根据字节大小转KB,MB
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ConvertBytes(long bytes)
        {
            string sizeContent = string.Empty;
            if (bytes < 1024.00f)
            {
                sizeContent = bytes.ToString("F2") + " B";
            }
            else if (bytes >= 1024.00f && bytes < 1048576)
            {
                sizeContent = (bytes / 1024.00).ToString("F2") + " KB";
            }
            else if (bytes >= 1048576 && bytes < 1073741824)
            {
                sizeContent = (bytes / 1024.00f / 1024.00f).ToString("F2") + " MB";
            }
            else if (bytes >= 1073741824)
            {
                sizeContent = (bytes / 1024.00f / 1024.00f / 1024.00f).ToString("F2") + " GB";
            }

            return sizeContent;
        }

        public static bool IsNumeric(string value)
        {
            return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
        }

        public static string[] SplitSpacing(string line)
        {
            string[] mm = Regex.Split(line, "\\s+", RegexOptions.IgnoreCase);
            return mm;
        }


        /// <summary>
        /// 分析 zzz:xxx这种字符串返回后面的xxx
        /// </summary>
        public static string LineByKeyPair(string left, string[] lines, char fuhao = '=')
        {
            foreach (var item in lines)
            {
                if (item.StartsWith(left))
                {
                    return item.Split(fuhao)[1].Trim('\r');
                }
            }

            return string.Empty;
        }

        /// <summary>
        /// 传入字符串,获得指定字符串之前的全部内容
        /// </summary>
        public static string SubstringStart(string val, string name, int offset = 0)
        {
            return val.Substring(0, val.LastIndexOf(name) + name.Length + offset);
        }

        /// <summary>
        /// 传入字符串,获得指定字符串之后的全部内容
        /// </summary>
        public static string SubstringEnd(string val, string name)
        {
            return val.Substring(val.LastIndexOf(name) + name.Length);
        }

        public static bool VersionJudge(string versionA, string versionB)
        {
            bool isResult = false;
            var verAry1 = versionA.Split('.');
            var verAry2 = versionB.Split('.');
            for (int i = 0; i < verAry1.Length; i++)
            {
                string val1 = verAry1[i];
                if (i == 0 && verAry1[i].StartsWith("v"))
                {
                    val1 = verAry1[i].Substring(1);
                }

                string val2 = verAry2[i];
                if (i == 0 && verAry2[i].StartsWith("v"))
                {
                    val2 = verAry2[i].Substring(1);
                }

                int num1 = Convert.ToInt32(val1);
                int num2 = Convert.ToInt32(val2);

                if (num1 > num2)
                {
                    isResult = true;
                }
            }

            return isResult;
        }
    }
}